package com.efast.dmom.mgmt.job;

import com.efast.cafe.commponent.job.base.AbstractStatefulJobBean;
import com.efast.cafe.commponent.job.bean.ScheduleExecuteLog;
import com.efast.cafe.commponent.log.ext.CafeLogger;
import com.efast.cafe.commponent.log.ext.CafeLoggerFactory;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.les.DpathWindowTimeBean;
import com.efast.dmom.mgmt.entity.les.TwBasDpathWindowTime;
import com.efast.dmom.mgmt.service.les.TwBasDpathWindowTimeService;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 窗口时间生成 Job
 * 
 * @author chlp
 */
public class GenerateWindowTimeJob extends AbstractStatefulJobBean {

	CafeLogger log = CafeLoggerFactory.getLogger(GenerateWindowTimeJob.class);
	
	
	/**
	 * 根据设置的窗口时间，每天定时将下一天的实际窗口时间创建出来 a):后台job，根据窗口时间配置自动执行； 窗口时间创建每天运行一次即可，因为每次会将所有下一天的窗口时间全部创建出来
	 * 
	 * 1)涉及数据表： 实际窗口时间 TW_BAS_DPATH_WINDOW_TIME 补货路径窗口时间配置 TR_BAS_DPATH_TIME路径 TM_BAS_DPATH 路径 TM_BAS_DPATH
	 */
	@Override
	protected synchronized void executeInternal(JobExecutionContext context,ScheduleExecuteLog cachelog) throws JobExecutionException {
		log.info("定时任务：根据设置的窗口时间，每天定时将下一天的实际窗口时间创建出来.....");
		TwBasDpathWindowTimeService twBasDpathWindowTimeService = SpringContextUtils.getBean(TwBasDpathWindowTimeService.class);
		TwBasDpathWindowTime twBasDpathWindowTimes = twBasDpathWindowTimeService.queryTwBasDpathWindowTime();
		List<DpathWindowTimeBean> list = twBasDpathWindowTimeService.queryWindowTime();
		// 说明是第一次生成。
		if (twBasDpathWindowTimes == null) {
			System.out.println(new Date() + "第一次生产窗口时间");
			generateTime(list,twBasDpathWindowTimeService);
		} else {
			System.out.println("不是第一次生成，则判断今天是否已经生成了");
			// 判断是否已经生成了。
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
			boolean ifGenerate = fmt.format(twBasDpathWindowTimes.getCreateTime()).equals(fmt.format(new Date()));
			if (!ifGenerate) {
				System.out.println("今天已经生成了........");
				generateTime(list,twBasDpathWindowTimeService);
			} else {
				System.out.println("今天已经生成了窗口时间，此次job不生成窗口时间");
			}
		}
	}

	private void generateTime(List<DpathWindowTimeBean> list,TwBasDpathWindowTimeService twBasDpathWindowTimeService) {
		for (DpathWindowTimeBean dpathWindowTimeBean : list) {
			TwBasDpathWindowTime twBasDpathWindowTime = new TwBasDpathWindowTime();
			twBasDpathWindowTime.setTmBasDpathId(dpathWindowTimeBean.getTmBasDpathId());
			Date date = new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			// +1今天的时间加一天
			calendar.add(Calendar.DAY_OF_MONTH, +1);
			date = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sdf.format(date);
			time = time + " " + dpathWindowTimeBean.getDpathTime();
			try {
				twBasDpathWindowTime.setDpathWindowTime(sdf1.parse(time));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			// 补货执行状态(0未执行，1已执行 )
			twBasDpathWindowTime.setFlag("0");
			twBasDpathWindowTime.setMarkStatus(1);
			twBasDpathWindowTime.setCreateTime(new Date());
			twBasDpathWindowTime.setCompanyCode(dpathWindowTimeBean.getCompanyCode());
			twBasDpathWindowTimeService.insertNonEmptyTwBasDpathWindowTime(twBasDpathWindowTime);
		}
	}

}